package com.dts.project.dao; 
import java.sql.*; 

import com.dts.core.dao.AbstractDataAccessObject;
import com.dts.core.util.CoreHash;
import com.dts.core.util.CoreList;
import com.dts.core.util.LoggerManager;
import com.yourcompany.struts.form.ProductMasterForm;

public class ProductMasterDao extends AbstractDataAccessObject 
{
	public boolean insertProductMaster(ProductMasterForm rModel)
	{
		java.sql.Connection con=null;
		boolean flag=false;
		try
		{
		int n=getSequenceID("ProductMaster","ProductId");
		con=getConnection();
		PreparedStatement ps=con.prepareStatement("insert into Productmaster values(?,?,?)");
		ps.setInt(1,n);
		ps.setString(2,rModel.getProductName());
		ps.setString(3,rModel.getProductDesc());		 
		int i=ps.executeUpdate();
		if(i>0)
			flag=true;
		 
			
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}finally
	{
		try{
			con.close();
		}
	catch(Exception e)
	{
		LoggerManager.writeLogInfo(e);
	}
	}
	return flag;
	
	
		}

public CoreList ViewProductMaster()
{   CoreList v=new CoreList();
	java.sql.Connection con=null;
	ProductMasterForm rModel=null;
	try
	{
		con=getConnection();
		Statement st=con.createStatement();
		ResultSet rs=st.executeQuery("select * from ProductMaster");
		 
		while(rs.next())
		{
			rModel=new ProductMasterForm();
			rModel.setProductId(rs.getInt(1));
			rModel.setProductName(rs.getString(2));
			rModel.setProductDesc(rs.getString(3));
			 
            v.add(rModel);
		}

		 
	}
		catch(Exception e)
	{
		
	}
		finally
		{
			try{
				con.close();
			}
		catch(Exception e)
		{
			LoggerManager.writeLogInfo(e);
		}
		}
		return v;
		
	
}
public ProductMasterForm ViewProductMasterById(int ProductId)
{   ProductMasterForm rModel=new ProductMasterForm();
	java.sql.Connection con=null;
	 
	try
	{
		 
		con=getConnection();
		Statement st=con.createStatement();
		PreparedStatement ps=con.prepareStatement("select * from ProductMaster where ProductId = ? ");
		ps.setInt(1,ProductId);
		ResultSet rs=ps.executeQuery();
		 
		if(rs.next())
		{
			rModel.setProductId(rs.getInt(1));
			rModel.setProductName(rs.getString(2));
			rModel.setProductDesc(rs.getString(3));
			
			 
		}

		 
	}
		catch(Exception e)
	{
		
	}finally
	{
		try{
			con.close();
		}
	catch(Exception e)
	{
		LoggerManager.writeLogInfo(e);
	}
	}
	return rModel;
	
	
}

public boolean deleteProductMaster(int ProductId)
	{
		java.sql.Connection con=null;
		boolean flag=false;
		try
		{
			 
			con=getConnection();
			 
			PreparedStatement ps=con.prepareStatement("delete from ProductMaster where ProductId = ? ");
			ps.setInt(1,ProductId);
			int i=ps.executeUpdate();
			if(i>0)
				flag=true;
			
			          
		}
		catch(Exception e)
		{
		}
		finally
		{
			try{
				con.close();
			}
		catch(Exception e)
		{
			LoggerManager.writeLogInfo(e);
		}
		}
		return flag;
		
		}

public boolean UpdateProductMaster(ProductMasterForm rModel)
{
	java.sql.Connection con=null;
	boolean flag=false;
	try
	{
	 
	con=getConnection();
	PreparedStatement ps=con.prepareStatement("update ProductMaster set ProductName = ?,ProductDesc = ? where ProductId = ? ");
	ps.setString(1,rModel.getProductName());
	ps.setString(2,rModel.getProductDesc());
	ps.setInt(3,rModel.getProductId());
	int i=ps.executeUpdate();
	if(i>0)
		flag=true;
	 
}
catch(Exception e)
{
	e.printStackTrace();
}finally
{
	try{
		con.close();
	}
catch(Exception e)
{
	LoggerManager.writeLogInfo(e);
}
}
return flag;


	}
public String getProductName(int productid)
{
	String productname="";
	Connection con=null;
	try{
		con=getConnection();
		PreparedStatement pst=con.prepareStatement("Select productname from productmaster where productid=?");
		pst.setInt(1,productid);
		ResultSet rs=pst.executeQuery();
		while(rs.next())
		{
			productname=rs.getString(1);
		}
	}catch (Exception e) {
		// TODO: handle exception
	}
	return productname;
}
public CoreHash listCategoryNames()
{
	CoreHash aCoreHash = new CoreHash();
	aCoreHash.clear();
	int categoryid;
	Statement st;
	Connection con=null;
	try {
		con = getConnection();
		st = con.createStatement();
		ResultSet rs = st.executeQuery("select productid,productname from productmaster order by productid");
		while(rs.next())
		{				
			aCoreHash.put(new Integer(rs.getInt(1)), rs.getString(2));
		}
	} 
	catch (SQLException e)
	{
		LoggerManager.writeLogWarning(e);
	}
	finally
	{
		try
		{
			con.close();
		}
		catch(SQLException se)
		{
			LoggerManager.writeLogWarning(se);
		}
	}
	return aCoreHash;
}



}






 
